import { _decorator, Component, EventKeyboard, Input, input, KeyCode, macro, Node, RigidBody2D, Vec2, Vec3 } from 'cc';
import { player } from './player';
const { ccclass, property } = _decorator;

@ccclass('playerController')
export class playerController extends Component {
    private player: player = null;
    private speed: number = 0;      // 玩家的速度
    private fastSpeedRatio: number = 0;     // 玩家快速移动倍数
    private direction: Vec2 = new Vec2(0, 0);   // 朝向
    private isMoving: boolean = false;      // 是否正在移动
    private attacking: boolean = false;     // 是否正在攻击
    private keyMap = {
        A: false,
        D: false,
        W: false,
        S: false,
        L_Shift: false,
    };     // 当前按下的键

    onLoad() {
        // 读取玩家的数值
        this.player = this.node.getComponent(player)!;
        this.speed = this.player.speed;
        this.fastSpeedRatio = this.player.fastSpeedRatio;

        // 设置监听事件
        input.on(Input.EventType.KEY_DOWN, this.onKeyDown, this);
        input.on(Input.EventType.KEY_UP, this.onKeyUp, this);
    }

    start() {
        
    }

    update(deltaTime: number) {
        // console.log(this.direction);
        // 更新角色位置
        // this.node.position.add3f(moveVec.x, moveVec.y, 0);
        if (this.isMoving) {
            let rb = this.node.getComponent(RigidBody2D)!;
            let moveRatio = this.speed;
            // 如果疾跑，还需要乘一个倍数
            if (this.keyMap.L_Shift) {
                moveRatio *= this.fastSpeedRatio;
                // 还需要减少玩家体力
                this.player.costStamina(10 * deltaTime);
            }
            rb.linearVelocity = this.direction.clone().multiplyScalar(this.keyMap.L_Shift ? this.speed * this.fastSpeedRatio : this.speed);
        }
        // 设置动画
    }

    onKeyDown(event: EventKeyboard) {
        // 判断x轴方向
        if (event.keyCode == KeyCode.KEY_A) {
            this.direction.x = -1;
            this.keyMap.A = true;
        } else if (event.keyCode == KeyCode.KEY_D) {
            this.direction.x = 1;
            this.keyMap.D = true;
        }
        // 判断y轴方向
        if (event.keyCode == KeyCode.KEY_W) {
            this.direction.y = 1;
            this.keyMap.W = true;
        } else if (event.keyCode == KeyCode.KEY_S) {
            this.direction.y = -1;
            this.keyMap.S = true;
        }

        // 监听shift（快速移动)
        if (event.keyCode == KeyCode.SHIFT_LEFT) {
            this.keyMap.L_Shift = true;
        }

        // 如果有方向键被按下，保持移动
        if (this.keyMap.A || this.keyMap.D || this.keyMap.W || this.keyMap.S) {
            this.isMoving = true;
        }
    }

    onKeyUp(event: EventKeyboard) {
        // 判断x轴方向
        if (event.keyCode == KeyCode.KEY_A) {
            this.direction.x = 0;
            this.keyMap.A = false;
        } 
        if (event.keyCode == KeyCode.KEY_D) {
            this.direction.x = 0;
            this.keyMap.D = false;
        } 
        // 判断y轴方向
        if (event.keyCode == KeyCode.KEY_W) {
            this.direction.y = 0;
            this.keyMap.W = false;
        }  
        if (event.keyCode == KeyCode.KEY_S) {
            this.direction.y = 0;
            this.keyMap.S = false;
        } 

        if (event.keyCode == KeyCode.SHIFT_LEFT) {
            this.keyMap.L_Shift = false;
        }
        
        // 如果没有方向键被按下，停止移动
        if (!this.keyMap.A && !this.keyMap.D && !this.keyMap.W && !this.keyMap.S) {
            this.isMoving = false;
        }
    }

    setMovingAnimation() {
        
    }
}


